//
//  HTestViewController.m
//  NursingManages
//
//  Created by Zhen Huang on 17/9/14.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "HTestViewController.h"
#import "HChooseBankViewController.h"
#import "DIHTest.h"
#import "HAnswerTableViewCell.h"
#import "HExamHeadView.h"
#import "HtestCollectionViewCell.h"
#import "SizeToFit.h"
#import <TMCache.h>
@interface HTestViewController ()
<UICollectionViewDelegate,
UICollectionViewDataSource,
UIScrollViewDelegate>
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, strong) HExamHeadView *headview;
@property (nonatomic, strong) UICollectionView *collecview;
@property (nonatomic, strong) NSMutableArray *answerArray;
@end

@implementation HTestViewController

- (instancetype)init
{
    if (self) {
        self.array = [NSMutableArray new];
        self.answerArray = [NSMutableArray new];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.button =[UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame=CGRectMake(20, 20, 100, 30);
    [self.button setTitle:@"切换题库" forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(showAllQuestions:) forControlEvents:UIControlEventTouchUpInside];
    
    self.button.layer.borderColor = [[UIColor whiteColor] CGColor];
    self.button.titleLabel.font = [UIFont systemFontOfSize:16];
    self.button.layer.borderWidth = 1.0f;
    
    UIBarButtonItem *execbtn = [[UIBarButtonItem alloc] initWithTitle:@"清空记录" style:UIBarButtonItemStylePlain target:self action:@selector(didDeleteButttonClicked:)];
    self.navigationItem.rightBarButtonItem = execbtn;
    
    NSString *bankid= [[TMCache sharedCache]objectForKey:@"bankid"];
    NSString *bankName = [[TMCache sharedCache]objectForKey:@"bankName"];
    if (bankid.length > 0) {
        DIHTest *test = [[DIHTest alloc]init];
        [self.array removeAllObjects];
        self.array = [test getQuesWithBankID:bankid];
        [self.button setTitle:bankName forState:UIControlStateNormal];
        self.collecview.contentSize = CGSizeMake(ScreenWidth * self.array.count, ScreenHeight);
        [self.collecview reloadData];

    }
    self.navigationItem.titleView = self.button;
    [self.view addSubview:self.collecview];
}

- (void)showAllQuestions:(UIButton *)sender
{
    HChooseBankViewController *HQCDVC = [[HChooseBankViewController alloc]init];
    UINavigationController *navHQCDVC = [[UINavigationController alloc]initWithRootViewController:HQCDVC];
    DIHTest *test = [[DIHTest alloc]init];
    HQCDVC.title = @"题库选择";
    HQCDVC.getBlock = ^(Model_Hr_Quesbank_Type *model) {
        [self.array removeAllObjects];
        self.array = [test getQuesWithBankID:model.BANKID];
        [[TMCache sharedCache]setObject:model.BANKID forKey:@"bankid"];
        [[TMCache sharedCache]setObject:model.BANKNAME forKey:@"bankName"];

        self.collecview.contentSize = CGSizeMake(ScreenWidth * self.array.count, ScreenHeight);
        [self.collecview reloadData];
        
        [self.button setTitle:model.BANKNAME forState:UIControlStateNormal];
        
    };
    [self presentViewController:navHQCDVC animated:YES completion:NULL];

}


- (UICollectionView *)collecview {
    if (!_collecview) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(ScreenWidth, ScreenHeight );
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        
        _collecview = [[UICollectionView alloc] initWithFrame:CGRectMake(0, NavBarHeight , ScreenWidth, ScreenHeight  ) collectionViewLayout:layout];
        _collecview.pagingEnabled = YES;
        _collecview.backgroundColor = [UIColor whiteColor];
        _collecview.showsHorizontalScrollIndicator = NO;
        _collecview.bounces = YES;
        _collecview.dataSource = self;
        _collecview.delegate = self;
        [_collecview registerClass:[HtestCollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
    }

    return _collecview;

}

// 设置单元格的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.array.count;
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HtestCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
    DIHTest *test = [[DIHTest alloc]init];
    [self.array removeAllObjects];
    self.array = [test getQuesWithBankID:[[TMCache sharedCache] objectForKey:@"bankid"]];
    Model_Hr_Question_Item *model = self.array [indexPath.row];
//    if (model.answers != nil) {
//        cell.userInteractionEnabled = NO;
//    }else {
//        cell.userInteractionEnabled = YES;
//    }
    NSMutableArray *array = [test modelWithTypeId:model.TYPEID];
    Model_Hr_Question_Type *typeModel = array[0];
    NSString *string  = [NSString stringWithFormat:@"(%@)\n%@",typeModel.QUETYPE,model.TITLE];
    CGFloat height = [SizeToFit getHeightByWidth:ScreenWidth - 20 title:string font:[UIFont systemFontOfSize:19]];

    [cell configure:indexPath.row dataArray:self.array headHeight:height + 20 headTitle:string typeid:model.TYPEID];
    
//    if (indexPath.item+1<self.array.count) {
//        
//        [self.collecview scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:indexPath.item + 1 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
//    }

    return cell;
}

- (void)shuaxin {
    [self.collecview reloadData];
}

- (void)didDeleteButttonClicked:(UIButton *)sender {
    DIHTest *test = [[DIHTest alloc]init];
    NSString *bankid = [[TMCache sharedCache]objectForKey:@"bankid"];
    [test updateDataWithBankid:bankid];
    [self.collecview reloadData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
